using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;

namespace UpdateSPUserProfileTimerJob.Features.UpdateSPUserProfileTimerJobFeature
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("b0f2295d-d21f-457a-8d51-16026cd60cda")]
    public class UpdateSPUserProfileTimerJobFeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        const string JOB_NAME = "UpdateSPUserProfileTimerJob";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // FIND THE MY SITE WEB APP
            SPSite site = properties.Feature.Parent as SPSite;

            SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite elevatedSite = new SPSite(site.ID))
                        {
                            if (elevatedSite != null)
                            {
                                SPWebApplication webApp = elevatedSite.WebApplication;

                                // delete the job if it already exists
                                foreach (SPJobDefinition job in webApp.JobDefinitions)
                                {
                                    if (job.Name == JOB_NAME)
                                    {
                                        job.Delete();
                                    }
                                }

                                // install the job
                                
                                UpdateSPUserProfileTimerJob newJob = new UpdateSPUserProfileTimerJob(JOB_NAME, webApp);
                                newJob.Properties.Add("SiteCollectionURL", elevatedSite.Url);
                                
                                // set a schedule to run the job hourly
                                SPDailySchedule schedule = new SPDailySchedule();
                                schedule.BeginHour = 23;
                                schedule.BeginMinute = 5;
                                schedule.BeginSecond = 0;
                                schedule.EndSecond = 15;
                                schedule.EndMinute = 5;
                                schedule.EndHour = 23;
                                newJob.Schedule = schedule;
                                // save changes
                                newJob.Update();
                            }
                    }
                    });
                }
       
        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite mySite = properties.Feature.Parent as SPSite;
                        if (mySite != null)
                        {
                            SPWebApplication webApp = mySite.WebApplication;

                            // delete the job if it already exists
                            foreach (SPJobDefinition job in webApp.JobDefinitions)
                            {
                                if (job.Name == JOB_NAME)
                                {
                                    job.Delete();
                                }
                            }
                        }
                    }
                }
            }
        


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    

